import { Injectable } from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import {Application} from "../model/application";
import {BackCode} from "../model/backCode";
import MTSConstants from "../config/config";
import {Purpose} from "../model/purpose";
import {Approve} from "../model/approve";

@Injectable()
export class CamApplyService {

  servicename:string;

  constructor(private http:Http) { }

  //新增车辆信息
  addApplication(postvalue:Application,fileoids:string):Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}application`;
    postvalue.starttime = new Date(postvalue.starttimeToString);
    postvalue.endtime = new Date(postvalue.endtimeToString);
    const body = postvalue;
    const headers = MTSConstants.headers;//spring的restful接口用这个
    if(headers.has("attachs"))
      headers.delete("attachs");
    if(fileoids==""){
      fileoids = "#";
    }
    headers.append("attachs",fileoids);
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('addApplication',error);});
  }

  //获取车辆申请信息
  getApplicationByPsn(psn:string){
    const url = `${MTSConstants.HOSTUser}application/psn/${psn}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getApplicationByPsn',error);});
  }

  //查询所有用车性质
  getPurposes():Promise<Purpose[]>{
    const url = `${MTSConstants.HOSTUser}purposes`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getPurposes',error);});
  }

  //查询所有用车记录（流程状态为审核中）
  getApplications(pageNumber:number,pageSize:number){
    const url = `${MTSConstants.HOSTUser}applications/vehicle?pageNumber=${pageNumber}&pageSize=${pageSize}&exclude=2,3,4&datelimit=y`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getApplications',error);});
  }

  //用车申请详情
  getApplicationDetails(processsn:string){
    const url = `${MTSConstants.HOSTUser}application/details?processsn=${processsn}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getApplicationDetails',error);});

  }

  //批量审批
  batchApprove(postvalue:Approve[],userId:string):Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}application/batchapprove/${userId}`;
    const body = postvalue;
    // const headers = MTSConstants.headers;//spring的restful接口用这个
    // if(headers.has("attachs"))
    //   headers.delete("attachs");
    // if(fileoids==""){
    //   fileoids = "#";
    // }
    // headers.append("attachs",fileoids);
    // const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('batchApprove',error);});

  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }
}
